package web;/**
 * @author 王俊彪
 * @version 1.0
 */

import beans.LoginStatus;
import utils.CookieUtils;
import utils.ResultUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/user/logout")
public class LogoutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //销毁会话
        request.getSession().invalidate();
        //同时销毁自动登录生成的cookie
        Cookie cookie = CookieUtils.getCookie("autoLogin", request.getCookies());
        if(cookie != null){
            cookie.setMaxAge(0);
           response.addCookie(cookie);
        }
        //响应登出的相关信息
        String responseData = ResultUtils.result(LoginStatus.LOGIN_OUT,null);
        response.getWriter().write(responseData);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
